package com.hzzh.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzzh.dao.DepartmentDAO;
import com.hzzh.service.EmployeeService;
import com.hzzh.vo.Department;
import com.hzzh.vo.Employee;

/**
 * Servlet implementation class RegistServlet
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		//获取注册页面填写的请求参数
		String employeename=request.getParameter("employeename");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		int deptid=Integer.parseInt(request.getParameter("deptid"));
		String phone=request.getParameter("phone");
		String email=request.getParameter("email");
		
		Employee employee=new Employee(employeename,username,password,deptid,phone,email,"0","2");
		EmployeeService service=new EmployeeService();
		int flag;
		try {
			flag = service.regist(employee);
			if(flag==1){
				request.setAttribute("msg", "注册成功，正在审核");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else{
				
				request.setAttribute("msg", "用户名已存在，请重新注册");
				DepartmentDAO dao=new DepartmentDAO();
				List<Department> departmentsList=dao.selectAll();
				request.setAttribute("departmentsList", departmentsList);
				request.getRequestDispatcher("register.jsp").forward(request, response);
			}
		}
		catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
	}
}